<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-新建${name_china}</title>
 <%@include file="/common/head.jsp" %>
<style>
.remarkcon{
    height: 77px;
    background: #fff;
    border: solid 1px #e1e6eb;
    border-bottom: 0;
}
</style>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>新建${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="addForm" method="post">
  <div id="left" style="width:80%;float:left">
	 <div class="form-wrapper">
		<div class="h4 distance-double">
	        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
	        <span class="h4-border"></span>
	    </div>
	    <div class="form-cont">
      <p class="form-cont-title">排序</p>
      <input type="text" class="summer-input" id="reorder"/>
      <input type="hidden" class="summer-input" id="type" value="weixin"/>
      </div>
      <div class="form-cont">
      <p class="form-cont-title">顶部图片</p>
      <input type="hidden" class="summer-input" id="topImg"/>
      </div>
      <div class="form-cont">
      <p class="form-cont-title">标识图</p>
      <input type="hidden" class="summer-input" id="img"/>
      </div>
      <div class="form-cont">
      <p class="form-cont-title">标题</p>
      <input type="text" class="summer-input" id="title"/>
      </div>
      <div class="form-cont">
      <p class="form-cont-title">是否展示</p>
      <input type="text" class="summer-input" id="isVisible"/>
      </div>
      <div class="form-cont" id="goodsList">
      </div>	
	 <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
  </div>
</form>

<div class="search-field">  
	<span class="field-cont">
        <span class="field-cont-title">商品名称</span>
        <input type="text" class="summer-input" id="goodsName"/>
    </span>
    <span class="field-cont">
        <span class="field-cont-title">商品类型</span>
        <input type="text" class="summer-input" id="spuPlatform"/>
    </span>
     <span class="field-cont">
        <span class="field-cont-title">商品分类</span>
        <input type="text" id="categoryId" name="categoryId" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[500]']"/> 
        
    </span><br/>
   
     <span class="field-cont">
        <span class="field-cont-title">是否爆款</span>
        <input type="text" class="summer-input" id="isHot"/>
    </span><br/>
     <span class="field-cont">
        <span class="field-cont-title">佣金排序</span>
        <input type="text" class="summer-input" id="wlCommission"/>
    </span>
     <span class="field-cont">
        <span class="field-cont-title">佣金比例排序</span>
        <input type="text" class="summer-input" id="wlCommissionShare"/>
    </span>
     <span class="field-cont">
        <span class="field-cont-title">时间</span>
        <input type="text" class="summer-input" id="createTime"/>
    </span>

    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>
<div class="remarkcon">
<div class="remarkbut" id="batchAddHot"><a>设置爆品</a></div>
</div>
<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <col width="10%">
	    <col width="10%">
	    <col width="30%">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="25%">	
	    <thead>
		     <tr>
		    <th><div class="summer-datagrid-cell"></div></th>
		    <th><div class="summer-datagrid-cell">排序</div></th>
		    <th><div class="summer-datagrid-cell">名称</div></th>
		    <th><div class="summer-datagrid-cell">图片</div></th>
		    <th><div class="summer-datagrid-cell">类型</div></th>
		    <th><div class="summer-datagrid-cell">分类</div></th>
		    <th><div class="summer-datagrid-cell">价钱（原价、券后价、佣金数）</div></th>
		    <th><div class="summer-datagrid-cell">时间</div></th>
			 </tr>
	    </thead>
    </table>
</div>
<script>
    var  wlcReorder,
   		 wlcsReorder,
    	 timeReorder,
    	 editData,
    	 goodsList,
    	 shareSpuIds,
    	 no,
    	 permission = CommonUtil.getPermission('${name_english}');
    $(function () {
    	
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:addHotZone']
        }));
    	editData = ${editData};
    	
     	 //初始化上传
        $("#img").filePlugin({
       	 type:'image'
        });
        $("#topImg").filePlugin({
       	 type:'image'
        });
    	//下拉列表初始化
    	
    	$('#isVisible').combobox({
             valueField : 'code',
             textField : 'name',
             url : COMMON_URL.getCode + '?type=T_DM_T_MARKET_SHARE_IS_VISIBLE'
         });
    	
      //保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:addHotZone"]', {isContinue: false},  submit);
    	
    	//返回按钮
    	$('#backBtn,#goBack').on('click',backMain);
    	
    	//商品设置
    		
    	//下拉列表初始化
   	 $('#spuPlatform').combobox({
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_SPU_PLATFORM'
        });
   	 $('#isHot').combobox({
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_IS_HOT'
        });
   	 //T_DM_T_GOODS_SPU_WL_COMMISSION
   	 $('#wlCommission').combobox({
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_WL_COMMISSION'
        });
   	 //T_DM_T_GOODS_SPU_WL_COMMISSION_SHARE
   	 $('#wlCommissionShare').combobox({
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_WL_COMMISSION_SHARE'
        });
   	 //T_DM_T_GOODS_SPU_CREATE_TIME
   	 $('#createTime').combobox({
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_CREATE_TIME'
        });
   	 
   	//初始化下拉选项  商品所属公司
   	 $('#categoryId').combobox({
            url : CONST_URL.GET_CATEGORY_ID, 
            valueField : 'id',
            async:false,
            textField : 'name'
        });
   	
   	
    	if(editData != no){
    		//爆品商品图片加载
         	var html = null;
        	for(var i=0;i<editData.length;i++){
        		var j = i + 1;
        		if(html == null){
        			html = '<div style="float:left;width:20%;">'
        				   +'<input type="hidden" id="spuId'+i+'" value="'+editData[i].spuId+'">'
        				   +'<div style="width:148px;height:148px"><img style="width:100%;height:100%;" src="'+editData[i].img+'"></div>'
        				   +'<div style="width:90%;height:100px;"><p>'+editData[i].goodsName+'</p></div>'
        				   +'<div><input type="text" id="shareReorder" value="'+j+'"></div>'
        				   +'</div>';
        		}else{
        			html = html +  '<div style="float:left;width:20%;">'
    				   +'<input type="hidden" id="spuId'+i+'" value="'+editData[i].spuId+'">'
    				   +'<div style="width:148px;height:148px"><img style="width:100%;height:100%;" src="'+editData[i].img+'"></div>'
    				   +'<div style="width:90%;height:100px;"><p>'+editData[i].goodsName+'</p></div>'
    				   +'<div><input type="text" id="shareReorder" value="'+j+'"></div>'
    				   +'</div>';
        		}
        	}
        	$("#goodsList").append(html);        	
    	}
    	//设置爆品
    		//设置爆品
		//批量设置爆品
    	$('#batchAddHot').on('click',tobatchAddHot);
    	
    	 $('#search').on('click', reloadTable);
         $('#reset').on('click', function() {
 			//重置文本框的值
 			$('#goodsName').val('');
 			$('#spuPlatform').combobox('clear');
 			$('#categoryId').combobox('clear');
 			$('#isJingping').val('');
 			$('#isHot').combobox('clear');
 			$('#wlCommission').combobox('clear');
 			$('#wlCommissionShare').combobox('clear');
 			$('#createTime').combobox('clear');
 			reloadTable();
 		});
         
 		createTable();
    
    	
    });
    //表单提交
    function submit(event) {
    	var reorder = $("#reorder").val();
    	var title = $("#title").val();
    	var img = $("#img").val();
    	var topImg = $("#topImg").val();
    	var type = $("#type").val();
    	var isVisible = $('#isVisible').combobox('getValue');
    	var spuIds = null;
    	var mainImages = null;
    	if(editData != no){
    		//遍历商品，获取商品IDs
        	for(var i=0;i<editData.length;i++){
        		if(editData.length == 1){
        			spuIds = editData[i].spuId;
        			mainImages = editData[i].img;
        		}else{
        			if(i != editData.length-1){
        				if(spuIds == null){
        	    			spuIds = editData[i].spuId+",";
        	    			mainImages = editData[i].img+",";
        	    		}else{
        	    			spuIds = spuIds + editData[i].spuId+",";
        	    			mainImages = mainImages + editData[i].img+",";
        	    		}
        			}else{
        				spuIds = spuIds + editData[i].spuId;
        				mainImages = mainImages + editData[i].img;
        			}
        		}
        		
        	}
    	}
    	//保存数据
         $("#addForm").form({
        	 buttons : ['#saveBtn'],
        	 ajax: {
                 url: CONST_URL.ADD_HOT_ZONE+"?reorder="+reorder+"&title="+title+"&img="+img+"&topImg="+topImg+"&spuIds="+spuIds+"&mainImages="+mainImages+"&type="+type+"&isVisible="+isVisible
             },
             onSubmitSuccess: function (data) {
            	 $.stip({
  	                type:'ok',//显示类型 有info、 ok、 danger
  	                text: '保存成功！',
  	                onBeforeHide: function () {
  	                	backMain();
  	               }
              	});
              }
         }).form("submit");
    }
    
    //商品
    // 重新加载表格
	function reloadTable() {
		wlcReorder = $('#wlCommission').combobox('getValue');	
		wlcsReorder = $('#wlCommissionShare').combobox('getValue');
		timeReorder = $('#createTime').combobox('getValue');
		createTable(wlcReorder,wlcsReorder,timeReorder);
		$('#dataList').datagrid('reload', {
			ajax : {
				param : {
					"goodsName": trimSpace($('#goodsName').val()),
					"spuPlatform":$('#spuPlatform').combobox('getValue'),
					"isJingping": trimSpace($('#isJingping').val()),
					"categoryId":$('#categoryId').combobox('getValue'),
					"isHot":$('#isHot').combobox('getValue')
				}
			}
		});
	}
	
	//创建表格
	function createTable(wlcReorder,wlcsReorder,timeReorder) {
		
		$('#dataList')
				.datagrid(
						{
							ajax : {
								url : CONST_URL.GOODS_LIST+"?wlcReorder="+wlcReorder+"&wlcsReorder="+wlcsReorder+"&timeReorder="+timeReorder,
								param : {	
									"goodsName": trimSpace($('#goodsName').val()),
									"spuPlatform":$('#spuPlatform').combobox('getValue'),
									"isJingping": trimSpace($('#isJingping').val()),
									"categoryId":$('#categoryId').combobox('getValue'),
									"isHot":$('#isHot').combobox('getValue')
								},
								success : function(data) { //成功 
									alert("Data Saved: " + msg);
								}
							},
							columns : [
									{
										field : 'id',
										hidden : true
									},
									{
										render : function(rowData){
											return '<input type="checkbox" id="id" name="id" value="'+rowData.id+'">';
										}
									},
									{
										field : 'reorder'
									},
									{
										field : 'goodsName'
									},
									{
										render : function(rowData){
											return '<img width="100" height="100" src="'+rowData.mainImageUrl+'">';
										}
									},
									{
										field : 'spuPlatform'
									},
									{
										field : 'categoryId'
									},
									{
										render : function(rowData){
											return '<p>原价：'+rowData.wlPrice+'</p><br/>'
												  +'<p>券后价：'+rowData.conPrice+'</p><br/>'
												  +'<p>佣金：'+rowData.wlCommission+'</p>';
										}
									},
									{
										field : 'createTime'
									}

							]
						});
	}
	
	//批量设置爆品
	 //批量设置爆品
    function tobatchAddHot(){
    	var id = $("input[name='id']:checked").serialize();
		window.location.href = CONST_URL.ADD_SET_HOT + "?id="+id;
    }
    //保存并继续清空form
    function resetForm(){
        //$('#xm,#zw,#cs,#bgdh,#sjhm').val('');
    }
    
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_HOT;
    }
    
   
    //请求url
    var CONST_URL = {
       	ADD_HOT_ZONE: basePath + "${common_url}addHotZone",
       	INIT_MAIN: basePath + '${common_url}initMarket',
		INIT_HOT: basePath + '${common_url}initHot',
		INIT_SHARE: basePath + '${common_url}initShare',
		DEL: basePath + '${common_url}deljx',
		MARKET_LIST: basePath + '${common_url}marketList',
		INIT_REORDER: basePath + '${common_url}initReorder',
		INIT_GOODS_LIST: basePath + '${common_url}initGoods',
		GET_CATEGORY_ID: basePath + '${common_url}getGoodsCategory',
		SET_HOT: basePath + '${common_url}setHot',
		SAVE_HOT: basePath + '${common_url}saveHot',
		DEL_HOT_GOODS: basePath + '${common_url}delHotGoods',
		INIT_EDIT_HOT: basePath + '${common_url}initEditHot',
		ADD_SET_HOT: basePath + '${common_url}addsetHot',
		GOODS_LIST: basePath + '${common_url}goodsList'
    };
    
</script>
</body>
</html>